package cn.vivi.classes.controller;

import cn.vivi.classes.service.ClassesService;
import cn.vivi.common.entity.Classes;
import cn.vivi.common.util.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @GetMapping("{id}")
    public Result<Classes> findById(@PathVariable String id) {
        return Result.success(classesService.findById(id));
    }

    @PostMapping
    public Result addClasses(@RequestBody Classes classes) {
        return classesService.addClasses(classes) ? Result.success(): Result.error("添加分类失败");
    }

    @GetMapping("/{current}/{size}")
    public Result<Page<Classes>> findClassesByPage(@PathVariable long current, @PathVariable long size) {
        return Result.success(classesService.findByPage(current, size));
    }

    @PutMapping
    public Result updateClasses(@RequestBody Classes classes) {
        return classesService.update(classes) ? Result.success() : Result.error("更新分类失败");
    }

    @DeleteMapping("{id}")
    public Result deleteClasses(@PathVariable String id) {
     return classesService.deleteClasses(id) ? Result.success() : Result.error("删除分类失败");
    }

}
